import {
  OrderStatus,
  OrderButtonTypes
} from '../config';
import {
  fetchOrders
} from '../../../api/order';
Page({
  page: {
    size: 10,
    num: 1,
  },

  data: {
    tabs: [{
        key: -1,
        text: '全部'
      },
      {
        key: OrderStatus.PAYMENT,
        text: '待出行',
        info: ''
      },
      {
        key: OrderStatus.DELIVERY,
        text: '出行中',
        info: ''
      }, {
        key: OrderStatus.APPLY,
        text: '退款',
        info: ''
      }, {
        key: OrderStatus.COMPLETE,
        text: '已完成',
        info: ''
      },
    ],
    curTab: -1,
    orderList: [],
    listLoading: 0,
    pullDownRefreshing: false,
    emptyImg: 'https://cdn-we-retail.ym.tencent.com/miniapp/order/empty-order-list.png',
    backRefresh: false,
    status: -1,
  },

  onLoad(query) {
    let status = parseInt(query.status);
    status = this.data.tabs.map((t) => t.key).includes(status) ? status : -1;
    this.init(status);
    //this.pullDownRefresh = this.selectComponent('#wr-pull-down-refresh');
  },
  init(status) {
    status = status !== undefined ? status : this.data.curTab;
    this.setData({
      status,
    });
    this.refreshList(status);
  },

  onShow() {
    if (!this.data.backRefresh) return;
    this.onRefresh();
    this.setData({
      backRefresh: false
    });
  },
  onPageScroll(e) {
    this.pullDownRefresh && this.pullDownRefresh.onPageScroll(e);
  },

  onPullDownRefresh_(e) {
    this.setData({
      pullDownRefreshing: true
    });
    this.refreshList(this.data.curTab).then(() => {
      this.setData({
        pullDownRefreshing: false
      });
    }).catch((err) => {
      this.setData({
        pullDownRefreshing: false
      });
      Promise.reject(err);
    });
  },

  getOrderList(statusCode = -1, reset = false) {
    const params = {
      pageSize: this.page.size,
      pageNum: this.page.num,
    };
    if (statusCode !== -1) params.status = statusCode;
    this.setData({
      listLoading: 1
    });
    return fetchOrders(params)
      .then((res) => {
        this.page.num++;
        let orderList = [];
        if (res && res.data && res.data.data) {
          orderList = (res.data.data || []).map((order) => {
            let mbuttons = []
            if (order.orderStatus === 1) {
              mbuttons = [{
                type: OrderButtonTypes.CANCEL,
                primary: false,
                name: '取消'
              }, {
                type: OrderButtonTypes.PAY,
                primary: true,
                name: '付款'
              }]
            }
            if (order.orderStatus === 2) {
              mbuttons = [{
                type: OrderButtonTypes.CONFIRM,
                primary: true,
                name: '确认上车'
              }, {
                type: OrderButtonTypes.REFUND,
                primary: false,
                name: '取消出行'
              }]
            }
            if (order.orderStatus === 3) {
              if (order.pay_status === 1) {
                mbuttons = [{
                  type: OrderButtonTypes.SERVICE,
                  primary: true,
                  name: '退款'
                }]
              } else if (order.pay_status === 2 || order.pay_status === 3) {
                mbuttons = [{
                  type: OrderButtonTypes.REVOKE,
                  primary: true,
                  name: '取消退款'
                }]
              }
            }
            if (order.orderStatus === 4) {
              if (order.pay_status === 1) {
                mbuttons = [{
                  type: OrderButtonTypes.SERVICE,
                  primary: true,
                  name: '退款'
                }]
              } else if (order.pay_status === 2 || order.pay_status === 3) {
                mbuttons = [{
                  type: OrderButtonTypes.REVOKE,
                  primary: true,
                  name: '取消退款'
                }]
              }
            }
            return {
              id: order.id,
              order_id: order.order_id,
              orderNo: order.orderNo,
              status: order.orderStatus,
              statusDesc: order.orderStatusName,
              amount: order.amount,
              createTime: order.createTime,
              buttons: mbuttons,
              goods: order.goodsInfo
            };
          });
        }
        return new Promise((resolve) => {
          if (reset) {
            this.setData({
              orderList: []
            }, () => resolve());
          } else resolve();
        }).then(() => {
          this.setData({
            orderList: this.data.orderList.concat(orderList),
            listLoading: orderList.length > 0 ? 0 : 2,
          });
        });
      })
      .catch((err) => {
        this.setData({
          listLoading: 3
        });
        return Promise.reject(err);
      });
  },

  onReTryLoad() {
    this.getOrderList(this.data.curTab);
  },

  onTabChange(e) {
    const {
      value
    } = e.detail;
    this.setData({
      status: value,
    });
    this.refreshList(value);
  },
  refreshList(status = -1) {
    this.page = {
      size: this.page.size,
      num: 1,
    };
    this.setData({
      curTab: status,
      orderList: []
    });
    return Promise.all([
      this.getOrderList(status, true),
      //this.getOrdersCount(),
    ]);
  },

  onRefresh() {
    this.refreshList(this.data.curTab);
  },

  onOrderCardTap(e) {
    /*
    const {
      order
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/order/order-detail/index?orderNo=${order.orderNo}`,
    });*/
  },
});